package com.iis.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import com.iis.entity.Agent;

public class EditAgentForm extends JFrame {

	private static EditAgentForm frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JTable dataTable = new JTable();
					frame = new EditAgentForm(dataTable);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EditAgentForm(JTable dataTable) {

		this.setTitle("Add New Agent");
		InitializeForm(null);

	}

	public EditAgentForm(JTable dataTable, Agent agent) {
		this.setTitle("Edit Existing Agent");
		InitializeForm(agent);

	}

	public void InitializeForm(Agent agent) {
		JPanel contentPane = new JPanel();
		contentPane.setLayout(null);
		contentPane.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED));

		JLabel lblID = new JLabel("Agent ID");
		lblID.setFont(lblID.getFont().deriveFont(12.0f));
		lblID.setBounds(40, 40, 100, 30);

		JLabel lblName = new JLabel("Name");
		lblName.setFont(lblID.getFont());
		lblName.setBounds(40, 80, 100, 30);

		JLabel lblRegion = new JLabel("Region");
		lblRegion.setFont(lblID.getFont());
		lblRegion.setBounds(40, 120, 100, 30);

		JTextField idTF = new JTextField();
		if (agent != null)
			idTF.setText(agent.getAgentNo());
		idTF.setColumns(20);
		idTF.setFont(idTF.getFont().deriveFont(14.0f));
		idTF.setBounds(190, 40, 220, 30);

		/*
		 * UtilDateModel model = new UtilDateModel(); Properties p = new
		 * Properties(); p.put("text.today", "Today"); p.put("text.month",
		 * "Month"); p.put("text.year", "Year"); JDatePanelImpl datePanel = new
		 * JDatePanelImpl(model, p); JDatePickerImpl datePicker = new
		 * JDatePickerImpl(datePanel, new DateLabelFormatter());
		 * datePicker.setBounds(190, 80, 220, 30);
		 */

		JTextField nameTF = new JTextField();
		if (agent != null)
			nameTF.setText(agent.getName());
		nameTF.setColumns(20);
		nameTF.setFont(idTF.getFont());
		nameTF.setBounds(190, 80, 220, 30);

		JTextField regionTF = new JTextField();
		if (agent != null)
			regionTF.setText(agent.getRegion());
		regionTF.setColumns(20);
		regionTF.setFont(idTF.getFont());
		regionTF.setBounds(190, 120, 220, 30);

		contentPane.add(lblID);
		contentPane.add(idTF);

		contentPane.add(lblName);
		contentPane.add(nameTF);

		contentPane.add(lblRegion);
		contentPane.add(regionTF);

		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(190, 160, 100, 40);
		contentPane.add(btnSubmit);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(310, 160, 100, 40);
		contentPane.add(btnCancel);

		setContentPane(contentPane);

		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 500, 280);

		frame = this;

		btnSubmit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if (agent != null) {
					agent.setAgentID(Long.parseLong(idTF.getText()));
					agent.setName(nameTF.getText());
					agent.setRegion(regionTF.getText());
					agent.update();
					JOptionPane.showMessageDialog(frame, "Berhasil Update");
					frame.setVisible(false);
				} else {
					Agent agent = new Agent();
					agent.setAgentID(Long.parseLong(idTF.getText()));
					agent.setName(nameTF.getText());
					agent.setRegion(regionTF.getText());
					agent.save();
					JOptionPane.showMessageDialog(frame, "Berhasil Simpan");
					frame.setVisible(false);
				}

			}
		});

		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.setVisible(false);
			}
		});
	}
}